package com.dlnu.sujiwords.service.Impl;

import com.dlnu.sujiwords.dao.Memories_numberMapper;
import com.dlnu.sujiwords.dao.ReviewMapper;
import com.dlnu.sujiwords.dao.UserMapper;
import com.dlnu.sujiwords.domain.client.AjaxResult;
import com.dlnu.sujiwords.domain.external.WordQuestion;
import com.dlnu.sujiwords.domain.pojo.EnWords;
import com.dlnu.sujiwords.domain.pojo.Review;
import com.dlnu.sujiwords.service.IReviewService;
import com.dlnu.sujiwords.util.constant.EnWordsConstant;
import com.dlnu.sujiwords.util.table.EnTableName;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Random;

@Service
public class ReviewServiceImpl implements IReviewService {
    @Resource
    private ReviewMapper reviewMapper;
    @Resource
    private Memories_numberMapper memoriesNumberMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    EnWordsServiceImpl enWordsService;

    @Override
    public AjaxResult getReviewMsg(String phoneNumber, Integer word_table) {

        return AjaxResult.success("成功获取复习数量",new String(
                "{\"hasNums\":"+ reviewMapper.hasReviewNums(phoneNumber,word_table)
                        +",\n\"masterNums\":"+ reviewMapper.hasMasterReviewNums(phoneNumber,word_table)
                        +",\n\"hasMemorizedNums\":"+memoriesNumberMapper.selectTableNumByTableNumAndPhoneNumber(phoneNumber, EnTableName.toMemorizesRowName(word_table))+"}")
            );
    }

    @Override
    public AjaxResult getReviewWord( int tableType,String pho) {
        int hasRem= memoriesNumberMapper.selectTableNumByTableNumAndPhoneNumber(pho,EnTableName.toMemorizesRowName(tableType));
        int hasRev = reviewMapper.hasReviewNums(pho,tableType);
        int hasMas = reviewMapper.hasMasterReviewNums(pho,tableType);
        EnWords word = null;
        try {
//            if((int)EnWordsConstant.class.getFields()[tableType].get(null) > hasRem){
                if(hasRev<hasRem){
                    if(hasRev > hasMas) {
                        Random r = new Random();
                        word = r.nextBoolean()?reviewMapper.getReviewWordsProcessA(pho, tableType, EnTableName.getTable(tableType)):reviewMapper.getMasterReviewWordsProcessBForNotMastered(pho,tableType);
                    }else {
                        word = reviewMapper.getReviewWordsProcessA(pho, tableType, EnTableName.getTable(tableType));
                    }
                }
                else {
                if(hasRev > hasMas) {
                    word =reviewMapper.getMasterReviewWordsProcessBForNotMastered(pho,tableType);
                }else{
                    word = reviewMapper.getMasterReviewWordsProcessBForMastered(pho,tableType);
                }
            }
            word.setId(-1);
            return AjaxResult.success("成功获取",new  WordQuestion(word,enWordsService.createWrongAnswer(3)));
        }catch (Exception e){
            System.out.println("获取词汇总数异常");
            return AjaxResult.success("fail to getReview");
        }
    }

    @Override
    public AjaxResult updateReview(Review review) {
        try {
            reviewMapper.DelReviewRecord(review.getUserId(),review.getWord(), review.getWordTable());
        }catch (Exception e){
            System.out.println("删除复习记录出错");
        }
        System.out.println(review);

        int result = reviewMapper.insertReviewRecord(review);
        return result > 0 ? AjaxResult.success("成功更新"):AjaxResult.success("更新失败");
    }
}
